package com.tjhzzx.danfossNiagaraCore.util;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.HashMap;

/**
 * TODO
 *
 * @author Junlin Zhang
 * @version 1.0
 * @date 10/9/2021 3:35 PM
 */
public class HttpClientUtil {

	public static String getMsg(String url, String userName, String password) {

		CookieStore cookieStore = new BasicCookieStore();
//		CloseableHttpClient httpClient = createSSLInsecureClient(cookieStore);
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();

		HttpGet httpGet = new HttpGet(url);
		CloseableHttpResponse response = null;

		// 由客户端执行(发送)Get请求
		try {
			RequestConfig requestConfig = RequestConfig.custom()
							// 设置连接超时时间(单位毫秒)
							.setConnectTimeout(5000)
							// 设置请求超时时间(单位毫秒)
							.setConnectionRequestTimeout(5000)
							// socket读写超时时间(单位毫秒)
							.setSocketTimeout(5000)
							// 设置是否允许重定向(默认为true)
							.setRedirectsEnabled(true).build();

			// 将上面的配置信息 运用到这个Get请求里
			httpGet.setConfig(requestConfig);
			String auth = getAuth(userName, password);
			httpGet.addHeader("Authorization", "Basic " + auth);
//			httpGet.addHeader("Cookie", "JSESSIONID=" + jSessionId);
//			httpGet.addHeader();
			response = httpClient.execute(httpGet);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
			String result = EntityUtils.toString(responseEntity);
			return result;

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		return null;

	}

	public static String postMsg(String url, String data) {

		CookieStore cookieStore = new BasicCookieStore();
//		CloseableHttpClient httpClient = createSSLInsecureClient(cookieStore);
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
		HttpPost httpPost = new HttpPost(url);
		CloseableHttpResponse response = null;


		// 由客户端执行(发送)Get请求
		try {
			RequestConfig requestConfig = RequestConfig.custom()
							// 设置连接超时时间(单位毫秒)
							.setConnectTimeout(5000)
							// 设置请求超时时间(单位毫秒)
							.setConnectionRequestTimeout(5000)
							// socket读写超时时间(单位毫秒)
							.setSocketTimeout(5000)
							// 设置是否允许重定向(默认为true)
							.setRedirectsEnabled(true).build();

			// 将上面的配置信息 运用到这个Get请求里
			httpPost.setConfig(requestConfig);
//			httpPost.addHeader("Cookie", "JSESSIONID=" + jSessionId);
			StringEntity requestEntity = new StringEntity(data, "utf-8");
			requestEntity.setContentEncoding("UTF-8");
			requestEntity.setContentType("application/json");
			httpPost.setEntity(requestEntity);
//			httpGet.addHeader();
//			String auth = getAuth(userName, password);
//			httpPost.addHeader("Authorization", "Basic " + auth);
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
			String result = EntityUtils.toString(responseEntity);
			return result;

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}

		return null;

	}

	public static String postFromMsg(String url, String userName, String password, HashMap<String, String> data) throws IOException {

		CookieStore cookieStore = new BasicCookieStore();
//		CloseableHttpClient httpClient = createSSLInsecureClient(cookieStore);
		CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
		HttpPost httpPost = new HttpPost(url);
		CloseableHttpResponse response = null;


		// 由客户端执行(发送)Get请求
		try {
			RequestConfig requestConfig = RequestConfig.custom()
							// 设置连接超时时间(单位毫秒)
							.setConnectTimeout(5000)
							// 设置请求超时时间(单位毫秒)
							.setConnectionRequestTimeout(5000)
							// socket读写超时时间(单位毫秒)
							.setSocketTimeout(5000)
							// 设置是否允许重定向(默认为true)
							.setRedirectsEnabled(true).build();

			// 将上面的配置信息 运用到这个Get请求里
			httpPost.setConfig(requestConfig);
//			httpPost.addHeader("Cookie", "JSESSIONID=" + jSessionId);
			ArrayList<NameValuePair> nameValuePairs = new ArrayList<>();
			data.entrySet().forEach(stringStringEntry -> {
				String key = stringStringEntry.getKey();
				String value = stringStringEntry.getValue();
				BasicNameValuePair basicNameValuePair = new BasicNameValuePair(key, value);
				nameValuePairs.add(basicNameValuePair);
			});
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, Consts.UTF_8));

//			httpGet.addHeader();
			String auth = getAuth(userName, password);
			httpPost.addHeader("Authorization", "Basic " + auth);
			response = httpClient.execute(httpPost);
			// 从响应模型中获取响应实体
			HttpEntity responseEntity = response.getEntity();
			String result = EntityUtils.toString(responseEntity);
			return result;

		}  finally {
			try {
				// 释放资源
				if (httpClient != null) {
					httpClient.close();
				}
				if (response != null) {
					response.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}

		}


	}


	private static String getAuth(String userName, String password) {
		String authorization = null;
		if (userName == null) {
			return null;
		}
		authorization = userName + ":" + ((password == null) ? "" : password);
		authorization = Base64.getEncoder().encodeToString(authorization.getBytes());
		return authorization;
	}

}


